package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.cloud.cmt.usermanage.base.BaseVaule;
import com.cloud.cmt.usermanage.entity.Organization;
import com.cloud.cmt.usermanage.entity.UserInfo;
import com.cloud.cmt.usermanage.entity.vo.OrganizationDetailVO;
import com.cloud.cmt.usermanage.service.OrganizationDetailRepository;
import com.cloud.cmt.usermanage.service.OrganizationRepository;
import com.cloud.cmt.usermanage.service.impl.OrgServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by wcy on 2017/10/24.
 */
@RestController
@RequestMapping("/org")
@Api(value = "/组织管理模块", description = "组织管理模块-组织机构增删查改等-by wcy")
public class OrganizationCtl {

    @Resource
    OrganizationRepository organizationRepository;

    @Resource
    OrganizationDetailRepository organizationDetailRepository;

    @Resource
    OrgServiceImpl orgService;

    @ApiOperation(value = "通过机构id获取详细信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResultMessage<OrganizationDetailVO> getUser(@PathVariable long id){
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setData(organizationDetailRepository.findOne(id));
        return resultMessage;
    }

    @ApiOperation(value = "保存机构信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<Organization> save(@RequestBody @Validated({Organization.SAVE.class}) Organization organization){
        ResultMessage resultMessage = new ResultMessage();
        organization = organizationRepository.save(organization);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setId(organization.getId()+"");

        return resultMessage;
    }

    @ApiOperation(value = "更新机构信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<Organization> update(@RequestBody Organization organization){
        ResultMessage resultMessage = new ResultMessage();
        Organization org = organizationRepository.save(organization);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return resultMessage;
    }

    @ApiOperation(value = "组织机构树查询")
    @GetMapping(value = "/getOrgTree")
    @ResponseBody
    public ResultMessage<List<Organization>> getOrgTree(){
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setData(orgService.getOrgTree(-1l));
        return resultMessage;
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        organizationRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return  resultMessage;
    }

    @ApiOperation(value = "通过搜索条件查询机构信息")
    @PostMapping(value = "/findOrg")
    public ResultMessage<List<Organization>> findBy(@RequestBody BaseVaule baseVaule) {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        List<Organization> organizations = organizationRepository.orgByFilds(baseVaule.getValue());
        resultMessage.setData(organizations);
        return  resultMessage;
    }
}
